package com.es.www.kyl.config;

import com.es.www.kyl.config.property.TokenFilterProperty;
import com.es.www.kyl.filter.CrossFilter;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.es.www.kyl.constant.FilterConstants;
import com.es.www.kyl.filter.TokenFilter;
import org.springframework.context.annotation.DependsOn;

import java.util.Arrays;
import java.util.List;

/**
 * @Author by michangtao
 * @Date 2023/3/21 20:15
 * @Description
 */
@Configuration
public class FilterConfig {



    @DependsOn("springContainerHolder")
    @Bean
    public TokenFilter tokenFilter(TokenFilterProperty tokenFilterProperty){
        TokenFilter tokenFilter = new TokenFilter();
        if(CollectionUtils.isNotEmpty(tokenFilterProperty.getExcludeUrls())) {
            tokenFilter.setExcludeUrls(tokenFilterProperty.getExcludeUrls());
        }
        return tokenFilter;
    }

    @Bean
    public FilterRegistrationBean<TokenFilter> tokenFilterRegistration(TokenFilter tokenFilter){
        FilterRegistrationBean<TokenFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(tokenFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(FilterConstants.ORDER_INIT_CONTEXT_FILTER);
        return filterRegistrationBean;
    }


    @Bean
    public FilterRegistrationBean<CrossFilter> myFilterRegistrationBean() {
        FilterRegistrationBean<CrossFilter> filterRegistrationBean = new FilterRegistrationBean(new CrossFilter());
        // 添加过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        // 保证在所有过滤器之前执行
        filterRegistrationBean.setOrder(Integer.MIN_VALUE);
        return filterRegistrationBean;
    }
}
